package com.mywiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mywiki.domain.Category;
import com.mywiki.domain.CategoryExample;
import com.mywiki.mapper.CategoryMapper;
import com.mywiki.req.CategoryQueryReq;
import com.mywiki.req.CategorySaveReq;
import com.mywiki.resp.CategoryQueryResp;
import com.mywiki.resp.PageResp;
import com.mywiki.utils.CopyUtil;
import com.mywiki.utils.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * describe:
 *
 * @author: robin
 * @date 2024/1/19 11:22
 */
@Service
public class CategoryService {

    private static final Logger LOG = LoggerFactory.getLogger(CategoryService.class);

    @Resource
    private CategoryMapper categoryMapper;

    @Resource
    private SnowFlake snowFlake;

    public List<CategoryQueryResp> listAll(){
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        List<Category> categorys = categoryMapper.selectByExample(categoryExample);
        List<CategoryQueryResp> categoryQueryResps = CopyUtil.copyList(categorys, CategoryQueryResp.class);
        return categoryQueryResps;
    }


    public PageResp<CategoryQueryResp> list(CategoryQueryReq req){

        PageHelper.startPage(req.getPage(), req.getSize());
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("sort asc");
        List<Category> categorys = categoryMapper.selectByExample(categoryExample);

        PageInfo<Category> pageInfo = new PageInfo<>(categorys);
        LOG.info("总行数: {}", pageInfo.getTotal());
        LOG.info("总页数: {}", pageInfo.getPages());

        //        List<categoryQueryResp> categoryQueryResps = new ArrayList<>();
        //        for(category category : categorys){
        //            categoryQueryResp resp = new categoryQueryResp();
        //            BeanUtils.copyProperties(category, resp);
        //            categoryQueryResps.add(resp);
        //        }
        List<CategoryQueryResp> categoryQueryResps = CopyUtil.copyList(categorys, CategoryQueryResp.class);

        PageResp<CategoryQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(categoryQueryResps);

        return pageResp;
    }

    /**
     * 保存category数据
     */
    public void save(CategorySaveReq req){
        Category category = CopyUtil.copy(req, Category.class);
        if(ObjectUtils.isEmpty(req.getId())){
            // 新增
            category.setId(snowFlake.nextId() );
            categoryMapper.insert(category);
        }
        else{
             categoryMapper.updateByPrimaryKey(category);
        }
    }

    public void delete(long id){
        categoryMapper.deleteByPrimaryKey(id);
    }
}
